export const buildBackground = (bg: any, paper: any) => {
    if (!bg || !bg.enablePreview) {
        return;
    }
    const style: Record<string, any> = {};
    if (bg.type === 'color') {
        style.background = bg.color;
    } else if (bg.type === 'image') {
        let layout = 'repeat';
        if (bg.imageLayout === 'repeat-x') {
            layout = 'repeat-x';
        } else if (bg.imageLayout === 'repeaty') {
            layout = 'repeat-y';
        } else if (bg.imageLayout === 'norepeat') {
            layout = 'no-repeat';
        }
        let size = 'initial';
        let bgWidth = '100%', bgHeight = '100%';
        if (bg.imageSize === 'hundredPercent') {
            size = '100% 100%';
        } else if (bg.imageSize === 'cover') {
            const width = paper.paperWidth - paper.marginLeft - paper.marginRight;
            bgWidth = (width * 3.8) + 'px';//mm to px
            size = width * 2.8346456693 + 'pt ';
            const height = paper.paperHeight - paper.marginTop - paper.marginBottom;
            bgHeight = (height * 3.8) + 'px';//mm to px
            size += height * 2.8346456693 + 'pt ';
        }

        style.url = `url("data:image/png;base64,${bg.imageData}")`;
        style.size = size;
        style.bgWidth = bgWidth;
        style.bgHeight = bgHeight;
        style.repeat = layout;
        style.opacity = bg.opacity || 1;
        style.position = `${0}px ${0}px`;
    }
    return style;
}

export type ReportInfo = {
    totalPages: number;
    tools: PreviewTool[];
}

export type PreviewTool = {
    name: string;
    type: string;
    icon: string;
    showLabel: boolean;
    showIcon: boolean;
}
export type ChangePreviewTool = PreviewTool & {
    fullDataLabel: string;
    pageDataLabel: string;
}

export type ExportItem = {
    type: string;
    label: string;
    icon: string;
    password: string;
    enable: boolean;
    exportPage: boolean;
    exportAll: boolean;
    fileNameType: 'template' | 'specify' | 'custom';
    exportFileName: string;
}
export type ExportPreviewTool = PreviewTool & {
    label: string;
    items: ExportItem[];
}
export type PrintPreviewTool = PreviewTool & {
    label: string;
    printPage: boolean;
    printAll: boolean;
    printWeb: boolean;
    printPageLabel: string;
    printAllLabel: string;
    printWebLabel: string;
}

export type PagePreviewTool = PreviewTool & {
    showFirstPageTool: boolean;
    showPrevPageTool: boolean;
    showNextPageTool: boolean;
    showLastPageTool: boolean;
    showCustomPageTool: boolean;
    firstPageLabel: string;
    prevPageLabel: string;
    nextPageLabel: string;
    lastPageLabel: string;
    customPageLabel: string;
}

export type LockRowCol = {
    class: string;
    display: boolean;
}

export type TableCell = {
    html: string;
    rowspan: number;
    colspan: number;
    style: string;
    class: string;
}

export type LockRow = {
    style: string;
    class: string;
    cells: TableCell[]
}